package fic.storefinder.client.content.composite;

import java.io.Serializable;
import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

import fic.storefinder.client.content.ContentAbstract;
import fic.storefinder.client.content.ContentContainer;
import fic.storefinder.client.content.EditPOIDialog;
import fic.storefinder.client.listener.UpdateDBListener;
import fic.storefinder.client.service.StoreService;
import fic.storefinder.client.service.StoreServiceAsync;
import fic.storefinder.shared.serializable.POI;
import fic.storefinder.shared.utils.Constants;

public class StoreListComposite extends ContentAbstract implements UpdateDBListener {
	
	private final StoreServiceAsync storeService = GWT.create(StoreService.class);
	private FlexTable flexTable;
	public StoreListComposite() {
		
		VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setSize("302", "");
		
		flexTable = new FlexTable();
		flexTable.setBorderWidth(1);
		verticalPanel.add(flexTable);
		flexTable.setSize("450px", "");
		
		Label lblNv = new Label("Név");
		flexTable.setWidget(0, 0, lblNv);
		
		Label lblCm = new Label("Cím");
		flexTable.setWidget(0, 1, lblCm);
		
		Label lblNewLabel = new Label("Műveletek");
		flexTable.setWidget(0, 2, lblNewLabel);
		flexTable.getFlexCellFormatter().setColSpan(0, 2, 2);
		flexTable.getCellFormatter().setVerticalAlignment(0, 2, HasVerticalAlignment.ALIGN_TOP);
		flexTable.getCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_TOP);
		flexTable.getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
		
		Button btnBolt = new Button("Új bolt felvétele");
		verticalPanel.add(btnBolt);
		btnBolt.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				EditPOIDialog newPOIDialog = new EditPOIDialog(Constants.EDIT_TYPE_NEW, null, StoreListComposite.this);
				newPOIDialog.center();
			}
		});
		this.listStores();
		
	}
	
	private void listStores() {
		storeService.getStoreList(new AsyncCallback<ArrayList<POI>>() {

			@Override
			public void onFailure(Throwable caught) {
				StoreListComposite.this.showDialog("Szerver oldali hiba", caught.getClass().getName() + ": " +  caught.getMessage());
			}

			@Override
			public void onSuccess(ArrayList<POI> result) {
				if (result == null) {
					return;
				}
				//remove prev rows from flextable:
				for (int i = 1; i < flexTable.getRowCount(); i++) {
					flexTable.removeRow(i);
				}
				
				int i = 1;
				
				for (final POI poi : result) {
					Label lblNv = new Label(poi.getPoiTitle());
					flexTable.setWidget(i, 0, lblNv);
					
					Label lblCm = new Label(poi.getAddress());
					flexTable.setWidget(i, 1, lblCm);
					
					Button btnEdit = new Button("Szerkesztés");
					flexTable.setWidget(i, 2, btnEdit);
					btnEdit.addClickHandler(new ClickHandler() {
						
						@Override
						public void onClick(ClickEvent event) {
							EditPOIDialog newPOIDialog = new EditPOIDialog(Constants.EDIT_TYPE_UPDATE, poi, StoreListComposite.this);
							newPOIDialog.center();
						}
					});
					
					Button btnRobes = new Button("Ruhák");
					flexTable.setWidget(i, 3, btnRobes);
					btnRobes.addClickHandler(new ClickHandler() {
						
						@Override
						public void onClick(ClickEvent event) {
							ContentContainer.getInstance().setPageContent(new RobeListComposite(poi));
						}
					});
					
					i++;
				}
			}
		});
	}

	/* (non-Javadoc)
	 * @see fic.storefinder.client.listener.UpdateDBListener#insert(java.io.Serializable)
	 */
	@Override
	public void insert(Serializable tableObject) {
		if (tableObject instanceof POI) {
			storeService.insertPOI((POI)tableObject, new AsyncCallback<String>() {

				@Override
				public void onFailure(Throwable caught) {
					StoreListComposite.this.showDialog("Szerver oldali hiba", caught.getClass().getName() + ": " +  caught.getMessage());
				}

				@Override
				public void onSuccess(String result) {
					StoreListComposite.this.listStores();
				}
			});
		}
		
	}

	/* (non-Javadoc)
	 * @see fic.storefinder.client.listener.UpdateDBListener#update(java.io.Serializable)
	 */
	@Override
	public void update(Serializable tableObject) {
		if (tableObject instanceof POI) {
			storeService.updatePOI((POI)tableObject, new AsyncCallback<String>() {

				@Override
				public void onFailure(Throwable caught) {
					StoreListComposite.this.showDialog("Szerver oldali hiba", caught.getClass().getName() + ": " +  caught.getMessage());
				}

				@Override
				public void onSuccess(String result) {
					StoreListComposite.this.listStores();
				}
			});
		}
	}

	/* (non-Javadoc)
	 * @see fic.storefinder.client.listener.UpdateDBListener#delete(java.io.Serializable)
	 */
	@Override
	public void delete(Serializable tableObject) {
		if (tableObject instanceof POI) {
			storeService.deletePOI((POI)tableObject, new AsyncCallback<String>() {

				@Override
				public void onFailure(Throwable caught) {
					StoreListComposite.this.showDialog("Szerver oldali hiba", caught.getClass().getName() + ": " +  caught.getMessage());
				}

				@Override
				public void onSuccess(String result) {
					StoreListComposite.this.listStores();
				}
			});
		}
	}

}
